
import { getList } from "../const/subApps"
// 给当前路由跳转打补丁
export const patchRouter = (globalEvent, eventName) => {
  return function() {
    const e = new Event(eventName)
    globalEvent.apply(this, arguments)
    window.dispatchEvent(e)
  }
}

export const getCurrentApp = () => {
  const currentUrl = window.location.pathname;
  return filterApp('activeRule', currentUrl)
}

export const findAppByRoute = (router) => {
  return filterApp('activeRule', router)
}

export const filterApp = (key,value) => {
  const apps = getList();
  let currentApp = apps.filter(item => item[key] == value)
  return currentApp && currentApp.length > 0? currentApp[0]: {}
}




// 判断子应用是否做了切换
export const isTurnChild = () => {
  // 当前子应用做个备份： 为了微前端框架中获取上一个路由 
  window.ORIGIN_APP = window.CURRENT_SUB_APP
  if( window.CURRENT_SUB_APP === window.location.pathname ) {
    return false
  } 
  let currentApp = window.location.pathname.match(/\/(\w+)/)[0]
  if(!currentApp) {
    return;
  }
  // 切换
  window.CURRENT_SUB_APP = currentApp;
  return true;
}